/* import SvgIcon from './SvgIcon/index.vue'

const components = { SvgIcon }

export default {
  install(app) {
    Object.keys(components).forEach((key) => {
      app.component(key, components[key])
    })
  }
}
 */

import { defineAsyncComponent } from 'vue'
//引入element-plus提供全部图标组件
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
// 默认一个文件夹文件自动注册全局组件
export default {
  install(app) {
    // import.meta.glob是vite的api
    const components = import.meta.glob('../components/**/*.vue')
    // 遍历组件模块实现自动注册
    for (const [key, value] of Object.entries(components)) {
      // 拼接组件注册的 name
      const componentNameArr = key.replace('./', '').replace('.vue', '').split('/')
      if (
        componentNameArr.length == 1 ||
        (componentNameArr.length > 1 && componentNameArr[componentNameArr.length - 1] == 'index')
      ) {
        const componentName = componentNameArr[0]
        // 通过 defineAsyncComponent 异步导入指定路径下的组件
        app.component(componentName, defineAsyncComponent(value))
      }
    }
    //将element-plus提供图标注册为全局组件
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
      app.component(key, component)
    }
  }
}
